package com.estate.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.estate.common.utils.DateUtils;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.system.domain.LentDetail;
import com.estate.system.domain.Lent;
import com.estate.system.mapper.LentDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.codec.cbor.Jackson2CborDecoder;
import org.springframework.stereotype.Service;
import com.estate.system.mapper.LentMapper;
import com.estate.system.domain.Lent;
import com.estate.system.service.ILentService;
import com.estate.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author estate
 * @date 2022-04-08
 */
@Service
public class LentServiceImpl implements ILentService
{
    @Autowired
    private LentMapper lentMapper;
    @Resource
    private LentDetailMapper lentDetailMapper;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Lent selectLentById(Long id)
    {
        Gson gson = new Gson();
        String lentJson = (String)redisUtil.get("lent:" + id);
        //有可能存在缓存穿透的问题
        Lent lent = new Lent();
        if (lentJson!=null && lentJson.equals("noExist")){
            System.out.println("bothnoExist1");
            return null;
        }else if(lentJson==null){
            lent=lentMapper.selectLentById(id);
            if (lent==null){
                redisUtil.set("lent:"+id,"noExist");
                System.out.println("bothnoExist");
                return null;
            }else{
                String s = gson.toJson(lent);
                redisUtil.set("lent:"+id,s);
                return lent;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回
            Lent lent1 = gson.fromJson(lentJson, Lent.class);

            return lent1;

        }
    }
    //一次性给出所有数据,是比较需要缓存,但是每次更新多少要删除这个所有数据
    /*
     * @onePageSize 一页多少数据
     * @pageNum   第几页
     * */
    @Override
    public PageInfo<Lent> selectAllLentList(int onePageSize, int pageNum) {




        Gson gson = new Gson();
        List allList = redisUtil.lGet("allLent:",0,-1);

        //有可能存在缓存穿透的问题

        if (allList!=null && allList.size()==1 && allList.get(0)=="noExist" ){
            System.out.println("bothnoExist1");
            return null;
        }else if(allList.size()==0){

            List<Lent> lentList1 = lentMapper.selectAllLentList();
            System.out.println("lentList1"+lentList1);
            System.out.println("hasSelectAllLentList");
            if (lentList1==null){
                redisUtil.lSet("allLent:","noExist");
                System.out.println("bothnoExist");
                return null;
            }else{

                for (Lent lent : lentList1) {
                    redisUtil.lSet("allLent:",lent);
                }
                PageInfo<Lent> lentPageInfo = redisLentPage(onePageSize, pageNum, lentList1);


                return lentPageInfo;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回,
            //分页后的结果,也是要知道分页的结果的....
            PageInfo<Lent> lentPageInfo = redisLentPage(onePageSize, pageNum, allList);
            return lentPageInfo;

        }

    }
    /**
     *
     * 不分页的redis查询
     *
     *
     * **/
    @Override
    public List<Lent> selectAllLentList() {


        Gson gson = new Gson();
        List allList = redisUtil.lGet("allLent:",0,-1);

        //有可能存在缓存穿透的问题

        if (allList!=null && allList.size()==1 && allList.get(0)=="noExist" ){
            System.out.println("bothnoExist1");
            return null;
        }else if(allList.size()==0){

            List<Lent> lentList1 = lentMapper.selectAllLentList();
            System.out.println("lentList1"+lentList1);
            System.out.println("hasSelectAllLentList");
            if (lentList1==null){
                redisUtil.lSet("allLent:","noExist");
                System.out.println("bothnoExist");
                return null;
            }else{

                for (Lent lent : lentList1) {
                    redisUtil.lSet("allLent:",lent);
                }
                List objects = redisUtil.lGet("allLent:",0,-1);


                return objects;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回,
            //分页后的结果

            return allList;

        }

    }
    @Override
    public List<Lent> selectAllLentList(Lent lent) {

        List<Lent> lentList1 = lentMapper.selectAllLentList();
        return lentList1;

    }
    /*
     * 给redis里面数据分页
     *
     * */

    public PageInfo<Lent> redisLentPage(int onePageSize, int pageNum,List objects){
        //分页
        //数据条数
        int selectNum;
        //计算全部分多少页
        int pagesize;
        int start;
        int end;

        //先计算总长度,然后全部多少页,页码是从哪里到哪里
//        List<Object> objects = redisUtil.lGet("allLent:", 0, -1);
        System.out.println(objects.size());
        //得到

        selectNum=objects.size();
        System.out.println("selectNum"+selectNum);
        //15/10===>1.5
        //得到分页页数
        if(selectNum%onePageSize==0){
            pagesize=selectNum/selectNum;

        }else{
            pagesize=(selectNum/selectNum)+1;

        }
        System.out.println(pagesize);
        if(pageNum==1){
            //得到当前第页的数据
            List objects1 = redisUtil.lGet("allLent:", 0, onePageSize-1);
            System.out.println("onePageSize"+objects1.size());
            PageInfo<Lent> lentPageInfo = new PageInfo<>();
            lentPageInfo.setList(objects1);
            lentPageInfo.setPages(pagesize);
            lentPageInfo.setTotal(objects.size());
            System.out.println("objects.size()"+objects.size());
            return lentPageInfo;
        }else {
            System.out.println("pageNum * onePageSize"+(pageNum-1) * onePageSize+",pageNum * onePageSize + onePageSize"+pageNum * onePageSize);
            List objects1 = redisUtil.lGet("allLent:", (pageNum-1) * onePageSize, pageNum * onePageSize-1);
            PageInfo<Lent> lentPageInfo = new PageInfo<>();
            lentPageInfo.setList(objects1);
            System.out.println("objects1.size()"+objects1.size());
            lentPageInfo.setPages(pagesize);
            lentPageInfo.setTotal(objects.size());
            return lentPageInfo;

        }


    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param lent 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Lent> selectLentList(Lent lent)
    {
        Gson gson = new Gson();
        String res = gson.toJson(lent);
        //如果redis有这个整个对象的数据,那么返回
        String redisString = (String)redisUtil.get(res);
        System.out.println(redisString);
        if(redisString!=null && redisString.equals("noExist")){
            return null;


        }else if(redisString!=null ){

            System.out.println("redisString");
            List<Lent> list= gson.fromJson(redisString, List.class);
            System.out.println("92"+list);
            return list;


        }else if (redisString==null){
            System.out.println("redisString==null");
            System.out.println(lent);

            List<Lent> list = lentMapper.selectLentList(lent);
            System.out.println("list"+list);
            if (list.size()==0){
                redisUtil.set(res,"noExist");
                System.out.println("noExist");
                return null;
            }else{
                String redisJson = gson.toJson(list);
                redisUtil.set(res,redisJson);
                return list;
            }

        }
        //这个有点麻烦呀....需要判断lent全部的东西
        return null;
    }
    @Override
    public List<Lent> selectLentListByMapper(Lent lent) {
        return lentMapper.selectLentList(lent);
    }

    public List<Lent> selectByTitleList(Lent lent)
    {
        return lentMapper.selectByTitleList(lent);
    }



    /**
     * 新增lent表一条记录
     * 新增lentdetail表一条同id的记录
     * 先插入到lent表,再查询lent表id
     * @param lent 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLent(Lent lent)
    {
        lent.setCreateTime(DateUtils.getNowDate());
        int res = lentMapper.insertLent(lent);
        if (res>0){
            redisUtil.del("allLent:");
        }

        List<Lent> lents = lentMapper.selectLentList(lent);
        Lent lent1 = lents.get(0);
        System.out.println(lent1);

        LentDetail lentDetail=new LentDetail();
        lentDetail.setId(lent.getId());
        lentDetail.setDetailPic("[]");
        lentDetail.setHeadpic("[]");
        lentDetail.setVideoAddress("[]");
        System.out.println("lent1.getId()"+lent1.getId());


        return lentDetailMapper.insertLentDetail(lentDetail);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param lent 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateLent(Lent lent)
    {
        System.out.println(lent);
        redisUtil.del("lent:"+lent.getId());
        redisUtil.del("allLent:");
        return lentMapper.updateLent(lent);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLentByIds(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        redisUtil.del("allLent:");
        return lentMapper.deleteLentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLentById(Long id)
    {
        int res = lentMapper.deleteLentById(id);
        if (res>0){
            redisUtil.del("lent:"+id);
            redisUtil.del("allLent:");
        }


        return res;
    }
}
